package com.woniuxy.kaer.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.system.mapper.ManagerMapper;
import com.woniuxy.kaer.system.mapper.RoleMapper;
import com.woniuxy.kaer.system.model.Manager;
import com.woniuxy.kaer.system.model.Role;
import com.woniuxy.kaer.system.model.RoleManager;
import com.woniuxy.kaer.system.mapper.RoleManagerMapper;
import com.woniuxy.kaer.system.service.RoleManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@Service
public class RoleManagerServiceImpl extends ServiceImpl<RoleManagerMapper, RoleManager> implements RoleManagerService {
    @Resource
    private RoleManagerMapper roleManagerMapper;
    @Resource
    private ManagerMapper  managerMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void addRole(Integer roleId, Integer managerId) throws Exception {
        Manager manager = managerMapper.selectById(managerId);
        if(manager == null){
            throw new Exception("管理员不存在");
        }
        Role role = roleMapper.selectById(roleId);
        if(role == null){
            throw new Exception("角色不存在");
        }
        QueryWrapper<RoleManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("manager_id", managerId);
        RoleManager roleManager = roleManagerMapper.selectOne(queryWrapper);
        if(roleManager == null){
            RoleManager roleManager1 = new RoleManager();
            roleManager1.setRoleId(roleId);
            roleManager1.setManagerId(managerId);
            roleManagerMapper.insert(roleManager1);
        }else{
            roleManager.setRoleId(roleId);
            roleManagerMapper.updateById(roleManager);
        }

    }

    @Override
    public void deleteRole(Integer roleId, Integer managerId) throws Exception {
        Manager manager = managerMapper.selectById(managerId);
        if(manager == null){
            throw new Exception("管理员不存在");
        }
        Role role = roleMapper.selectById(roleId);
        if(role == null){
            throw new Exception("角色不存在");
        }
        QueryWrapper<RoleManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("manager_id", managerId);
        RoleManager roleManager = roleManagerMapper.selectOne(queryWrapper);
        if(roleManager == null){
            throw new Exception("管理员不存在该角色");
        }
        roleManagerMapper.delete(queryWrapper);
    }

    @Override
    public void addRoleForOther(Integer roleId, Integer managerId, Integer otherId) throws Exception {
        Manager manager = managerMapper.selectById(managerId);
        System.out.println(manager+"231");
        if(manager == null){
            throw new Exception("管理员不存在");
        }
        Role role = roleMapper.selectById(roleId);
        if(role == null){
            throw new Exception("角色不存在");
        }
        QueryWrapper<RoleManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("manager_id", managerId);
        RoleManager roleManager = roleManagerMapper.selectOne(queryWrapper);
        if(roleManager == null){
            RoleManager roleManager1 = new RoleManager();
            roleManager1.setRoleId(roleId);
            roleManager1.setManagerId(managerId);
            roleManagerMapper.insert(roleManager1);
        }else{
            roleManager.setRoleId(roleId);
            roleManagerMapper.updateById(roleManager);
        }

    }
}
